package com.briup.common.config;

import com.briup.common.jwt.JwtInterceptor;
import com.briup.common.properties.JwtIntercepterConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * jwt拦截器的配置类
 */
@ConditionalOnProperty(prefix = "briup.config.jwt",name = "enabled",havingValue = "true")
@Configuration
public class WebJwtIntercepterAutoConfig implements WebMvcConfigurer {

	@Autowired
	private JwtIntercepterConfigProperties jwtIntercepterConfigProperties;

	@Autowired
	private RestTemplate restTemplate;

	// 添加jwt拦截器,并指定拦截路径
	// 例如，/auth/**
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(jwtInterceptor())
				.addPathPatterns(jwtIntercepterConfigProperties.getAuthPath());
	}

	// jwt拦截器
	@Bean
	public JwtInterceptor jwtInterceptor() {
		return new JwtInterceptor(restTemplate,jwtIntercepterConfigProperties.getRemoteAuthCheckUrl());
	}


}
